package com.example.administrator.baikangxing.activity;

import android.content.Context;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidadvance.topsnackbar.TSnackbar;
import com.example.administrator.baikangxing.MyApplication;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.interfaces.UiOpration;
import com.example.administrator.baikangxing.utils.NetworkHelper;

import java.lang.reflect.Field;

public abstract class BaseActivity extends FragmentActivity implements UiOpration {
    public View childView;
    public Context context;
    public TextView base_tv_title;
    public TextView base_tv_back;
    public ImageButton base_ib_menu;
    private FrameLayout base_fl_content;
    public LinearLayout base_ll_title;
    private boolean isImmersive = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉标题栏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//防止软件盘弹出,使界面变形
        setContentView(R.layout.activity_base);
        MyApplication.getInstance().addActivity(this);
        context = this;
        //设置带标题栏的基础布局
        base_tv_title = findView(R.id.base_tv_title);
        base_tv_back = findView(R.id.base_tv_back);
        base_ib_menu = findView(R.id.base_ib_menu);
        base_fl_content = findView(R.id.base_fl_content);
        base_ll_title = findView(R.id.base_ll_title);
        childView = View.inflate(this, getContentViewLayoutId(), null);
        setContainer(childView);
        base_tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initView();
        initData();
        initListener();
        //当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            LinearLayout linear_bar = (LinearLayout) findViewById(R.id.base_ll_bar);
            linear_bar.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linear_bar.getLayoutParams();
            params.height = statusHeight;
            linear_bar.setLayoutParams(params);
            linear_bar.setBackground(base_ll_title.getBackground());
        }
        boolean networkConnected = NetworkHelper.isNetworkConnected(MyApplication.context);
        if (!networkConnected) {
            TSnackbar snackbar = TSnackbar.make(base_fl_content, "无网络连接", TSnackbar.LENGTH_SHORT);
            snackbar.setActionTextColor(getResources().getColor(R.color.register_bg));
            View view = snackbar.getView();
            view.setBackgroundColor(getResources().getColor(R.color.white));
            snackbar.show();
        }
    }

    /**
     * 通过反射的方式获取状态栏高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void setContainer(View child) {
        // 每次添加之前移除所有的子view
        base_fl_content.removeAllViews();
        base_fl_content.addView(child);
    }

    // 设置标题
    public void setTitles(String title) {
        base_tv_title.setText(title);
    }

    // 设置标题栏右边的按钮
    public void setRightIcon(int srcId) {
        base_ib_menu.setImageResource(srcId);
    }

    // 设置标题栏左边的按钮
    public void setLeftIcon(int srcId) {
        base_tv_back.setText("");
        base_tv_back.setBackgroundResource(srcId);
    }

    public <T> T findView(int id) {
        T view = (T) findViewById(id);
        return view;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }
}
